var treeStyleJson = new Object();
treeStyleJson['Initial Isolate'] = 'background: white';
treeStyleJson['Homozygous Diploid'] = 'background: pink';
treeStyleJson['HO- Hemizygous'] = 'background: orange';
treeStyleJson['HO- Haploid'] = 'background: yellow';
treeStyleJson['SPS2-GFP Tagged KAN'] = 'background: lavender';
treeStyleJson['SPS2-GFP Tagged NAT'] = 'background: cyan';
treeStyleJson['KAN Haploid Candidate'] = 'background: lavender';
treeStyleJson['NAT Haploid Candidate'] = 'background: cyan';
treeStyleJson['Not Natural Variant'] = 'border: 3px dotted red';
treeStyleJson['Not S. cerevisiae'] = 'border: 3px dotted white';
treeStyleJson['Undetermined'] = 'border: 3px dotted black';

var displayNonStatus = false;
function drawVisualization(yoHash) {
    // Create and populate the data table.
    //need to traverse up if the state is not Initial Isolate to find parents and their siblings, until initial isolate
    //need to traverse down if the state is not Haploid Candidate
    //for each sibling, need to traverse downstream
    displayNonStatus = false;
    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn('string', 'Name');
    dataTable.addColumn('string', 'Parent');
    dataTable.addColumn('string', 'ToolTip');
    var nodeJsonArray = new Array();
    var currentJson = new Object();
    currentJson["key"] = yoHash.name;
    currentJson["status"] = yoHash.status;
    currentJson["parents"] = yoHash.strainParents;
    currentJson["alias"] = yoHash.alias;
    nodeJsonArray.push(currentJson);
    var downStreamStrains = new Array();
    var siblings = yoHash.siblings;
    if (siblings == null) {
        siblings = "";
    }
    var siblingArray = siblings.split("_");
    for (var sa = 0; sa < siblingArray.length; sa++) {
        if (siblingArray[sa] != null && siblingArray[sa].length > 1) {
            downStreamStrains.push(siblingArray[sa]);
        }
    }
    downStreamStrains.push(yoHash.name);
    getDownstreamStrains(nodeJsonArray, downStreamStrains, function() {
        var state = yoHash.status;
        if (state != null //&& state != "Initial Isolate"
        && state != "Undetermined" && state.indexOf("Not") == -1) {
            lookupSiblingStrains(nodeJsonArray, siblingArray, function() {
                getUpstreamStrains(nodeJsonArray, yoHash, function() {
                    populateDataTable(dataTable, nodeJsonArray, yoHash.name);
                    populateLegend(dataTable);
                    populateNonStateLegend(dataTable);
                    var familyTree = new google.visualization.OrgChart(document.getElementById('panel-labels'));
                    familyTree.draw(dataTable, {allowHtml: true, size: 'small'});
                    google.visualization.events.addListener(familyTree, 'select', function() {
                    if (familyTree.getSelection()[0]){
                        var key = dataTable.getValue(familyTree.getSelection()[0].row, 0);
                        key = key.replace(/\*/g, "");
                        if (key.indexOf("YO") != -1) {
                            selectStrain(key);
                        }
                    }
                    });
                    metaPanel.expand();
                    labelsPanel.expand();
                });
            });
        } else {
            if (state == null || state == "Undetermined" || state.indexOf("Not") != -1) {
                displayNonStatus = true;
            }
            populateDataTable(dataTable, nodeJsonArray, yoHash.name);
            populateLegend(dataTable);
            populateNonStateLegend(dataTable);
            var familyTree = new google.visualization.OrgChart(document.getElementById('panel-labels'));
            familyTree.draw(dataTable, {allowHtml: true, size: 'small'});
            google.visualization.events.addListener(familyTree, 'select', function() {
                if (familyTree.getSelection()[0]){
                    var key = dataTable.getValue(familyTree.getSelection()[0].row, 0);
                    key = key.replace(/\*/g, "");
                    if (key.indexOf("YO") != -1) {
                        selectStrain(key);
                    }
                }
            });
            metaPanel.expand();
            labelsPanel.expand();
        }
    });
}

function populateDataTable(dataTable, nodeJsonArray, yoKey) {
    var legendSize = 8;
    var nonStatusLegend = 0;
    if (displayNonStatus) {
        nonStatusLegend = 3;
    }
    var lastNode = nodeJsonArray[nodeJsonArray.length -1];
    var secondLastNode = nodeJsonArray[nodeJsonArray.length -2];
    //if (secondLastNode.parents != lastNode.key){
    //    alert("need to push last node");
    //}
    var totalNodes = legendSize + nonStatusLegend + nodeJsonArray.length;
    dataTable.addRows(totalNodes);
    var beginReverse = nodeJsonArray.length - 1;
    for (var r = beginReverse; r > -1; r--) {
        var nodeJson = nodeJsonArray[r];
        var nodeStatus = nodeJson.status;
        if (nodeStatus != null && nodeStatus.indexOf("Candidate") != -1) {
            dataTable.setCell(r + legendSize, 0, "*" + nodeJson.key + "*");
        } else {
            dataTable.setCell(r + legendSize, 0, nodeJson.key);
        }
        if (nodeStatus != null && nodeStatus != "Initial Isolate") {
            dataTable.setCell(r + legendSize, 1, nodeJson.parents);
        }
        if (nodeJson.key == yoKey) {
            dataTable.setRowProperty(r + legendSize, 'style', 'background: white');
        }
        dataTable.setCell(r + legendSize, 2, nodeJson.alias);
        //style
        var node_style = treeStyleJson[nodeStatus];
        dataTable.setRowProperty(r + legendSize, 'style',  node_style);
	    //top parent
	    /*if (r == beginReverse){
		    //dataTable.setCell(r + legendSize, 1, nodeJson.parents);
		    dataTable.setCell(totalNodes-1, 0, nodeJson.parents);
		    dataTable.setRowProperty(totalNodes-1 , 'style', 'background: white');
	        //dataTable.setCell(totalNodes-1, 2, 'Initial Isolate');
    	} */
    }
}
/*
function populateExampleData(dataTable, yoKey) {
    dataTable.addRows(20);
    dataTable.setCell(11, 0, yoKey);
    dataTable.setCell(11, 2, 'Initial Isolate');
    dataTable.setCell(17, 0, 'YO 601');
    dataTable.setCell(17, 1, yoKey);
    dataTable.setCell(9, 0, 'YO 602');
    dataTable.setCell(9, 1, yoKey);
    dataTable.setCell(10, 0, 'YO 559');
    dataTable.setCell(10, 1, 'YO 601');
    dataTable.setCell(10, 2, 'My Alias');
    dataTable.setCell(15, 0, 'YO 562');
    dataTable.setCell(15, 1, 'YO 601');
    dataTable.setCell(16, 0, 'YO 560');
    dataTable.setCell(16, 1, 'YO 601');
    dataTable.setCell(18, 0, 'YO 561');
    dataTable.setCell(18, 1, 'YO 601');
    dataTable.setCell(12, 0, 'YO 478');
    dataTable.setCell(12, 1, 'YO 559');
    dataTable.setCell(13, 0, 'YO 479');
    dataTable.setCell(13, 1, 'YO 559');
    dataTable.setCell(14, 0, 'YO 503');
    dataTable.setCell(14, 1, 'YO 560');
    dataTable.setCell(19, 0, '*YO 502*');
    dataTable.setCell(19, 1, 'YO 560');
    dataTable.setRowProperty(11, 'style', 'background: white');
    var statesIncluded = new Object();
    statesIncluded["Initial Isolate"] = "";
    var siblings = new Object();
    siblings["YO 560"] = "YO 561";
}
*/
function populateLegend(dataTable) {
    dataTable.setCell(0, 0, 'Initial Isolate');
    dataTable.setCell(1, 0, 'Homozygous Diploid');
    dataTable.setCell(1, 1, 'Initial Isolate');
    dataTable.setCell(2, 0, 'HO- Haploid');
    dataTable.setCell(2, 1, 'HO- Hemizygous');
    dataTable.setCell(3, 0, 'SPS2-GFP Tagged KAN');
    dataTable.setCell(3, 1, 'HO- Haploid');
    dataTable.setCell(4, 0, 'SPS2-GFP Tagged NAT');
    dataTable.setCell(4, 1, 'HO- Haploid');
    dataTable.setCell(5, 0, '*NAT Haploid Candidate*');
    dataTable.setCell(5, 1, 'SPS2-GFP Tagged NAT');
    dataTable.setCell(6, 0, '*KAN Haploid Candidate*');
    dataTable.setCell(6, 1, 'SPS2-GFP Tagged KAN');
    dataTable.setCell(7, 0, 'HO- Hemizygous');
    dataTable.setCell(7, 1, 'Homozygous Diploid');
    var ii_style = treeStyleJson['Initial Isolate'];
    dataTable.setRowProperty(0, 'style', ii_style);//treeStyleJson['Initial Isolate']);
    dataTable.setRowProperty(1, 'style', treeStyleJson['Homozygous Diploid']);
    dataTable.setRowProperty(2, 'style', treeStyleJson['HO- Haploid']);
    dataTable.setRowProperty(3, 'style', treeStyleJson['SPS2-GFP Tagged KAN']);
    dataTable.setRowProperty(4, 'style', treeStyleJson['SPS2-GFP Tagged NAT']);
    dataTable.setRowProperty(5, 'style', treeStyleJson['NAT Haploid Candidate']);
    dataTable.setRowProperty(6, 'style', treeStyleJson['KAN Haploid Candidate']);
    dataTable.setRowProperty(7, 'style', treeStyleJson['HO- Hemizygous']);
}

function populateNonStateLegend(dataTable) {
    if (displayNonStatus) {
        var nodeSize = dataTable.getNumberOfRows();
        dataTable.setCell(nodeSize - 3, 0, 'Not Natural Variant');
        dataTable.setCell(nodeSize - 2, 0, 'Not S.C.');
        dataTable.setCell(nodeSize - 2, 1, 'Not Natural Variant');
        dataTable.setCell(nodeSize - 1, 0, 'Undetermined');
        dataTable.setCell(nodeSize - 1, 1, 'Not S.C.');
        dataTable.setRowProperty(nodeSize - 3, 'style', treeStyleJson['Not Natural Variant']);
        dataTable.setRowProperty(nodeSize - 2, 'style', treeStyleJson['Not S.C.']);
        dataTable.setRowProperty(nodeSize - 1, 'style', treeStyleJson['Undetermined']);
    }

}

